<?php

namespace app\admin\controller;
use think\Request;
use app\admin\service\TreeService;
use app\common\model\Menu as menuModel;
use app\common\lib\Util;

class Menu extends Base
{
    /**
     * @param TreeService $service
     * @return mixed  菜单列表
     */
    public function index(TreeService $service){
        $menuList = $service->getMenuList();
        $this->assign('list',$menuList);
        $this->assign('title','菜单管理');
        return $this->fetch();
    }

    /**
     * @param $pid
     * @param TreeService $service
     * @return mixed 新增page
     */
    public function create($pid,TreeService $service)
    {
        $menuList = $service->getMenuList();
        $this->assign('list',$menuList);
        $this->assign('pid',$pid);
        return $this->fetch();
    }

    /**
     * @param Request $request
     * @param menuModel $model 新增action
     */
    public function store(Request $request,menuModel $model)
    {
        $menuName = $request->menu_name;
        if(empty($menuName)&&$menuName!='0'){
            return Util::apiResponse([],'菜单名不能为空',-1);
        }
        $menuData = [
            'name'=>$menuName,
            'icon_name'=>$request->icon_name,
            'parent_id'=>intval($request->parent_id)?$request->parent_id:null,
            'jump_url'=>$request->jump_url,
            'is_hide'=>intval($request->is_hide),
            'remark'=>$request->remark
        ];
        $ins = $model->save($menuData);
        if(!$ins){
            return Util::apiResponse([],'新增失败',-1);
        }
        return Util::apiResponse([],'新增成功',1);
    }

    /**
     * @param $id
     * @param TreeService $service
     * @param menuModel $model
     * @return mixed 更新page
     */
    public function edit($id,TreeService $service,menuModel $model){
        $menuInfo = $model->find($id);
        $menuList = $service->getMenuList();
        if (!$menuInfo) {
            $this->error( '该记录不存在');
        }
        $this->assign('menu_info',$menuInfo);
        $this->assign('list',$menuList);
        $this->assign('id',$id);
        return $this->fetch();
    }

    /**
     * @param $id
     * @param Request $request
     * @param menuModel $model 更新action
     */
    public function update($id,Request $request,menuModel $model){
        $menuName = $request->menu_name;
        if(empty($menuName)&&$menuName!='0'){
            return Util::apiResponse([],'菜单名不能为空',-1);
        }
        $menuData = [
            'id'=>$id,
            'name'=>$menuName,
            'icon_name'=>$request->icon_name,
            'parent_id'=>intval($request->parent_id)?$request->parent_id:null,
            'jump_url'=>$request->jump_url,
            'is_hide'=>intval($request->is_hide),
            'remark'=>$request->remark
        ];
        $upd = $model->save($menuData,['id'=>$id]);
        if(!$upd){
            return Util::apiResponse([],'更新失败',-1);
        }
        return Util::apiResponse([],'',1);
    }

    /**
     * @param $id
     * @param Request $request
     * @param menuModel $model 删除
     */
    public function delete($id,MenuModel $model)
    {
        $exist = $model->find($id);
        if (!$exist) {
            return Util::apiResponse([], '该记录不存在', -1);
        }
        $res = $model->destroy($id);
        if(!$res){
            return Util::apiResponse([],'删除失败',-1);
        }
        return Util::apiResponse([],'',1);
    }
}
